<template>
   <PanelSplitTwo bottomToolbarHeight="0" :num="2">
    <template #top>
      <SetUserSelect
        :key="keys"
        class="queryForm"
        :formData="orderSelect"
        v-model:queryParams="paginationParams"
        @selectFunc="selectFunc"
        @selChangeFun="selChangeFun"
      ></SetUserSelect>
    </template>
     <template #centerToolbar>
        <div class="vex_flex">
          <el-row :gutter="20" style="margin-bottom: 10px">
            <!-- <el-col :span="1.5">
              <el-button @click="handleQuery">新增</el-button>
            </el-col> -->
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="handleQuery(1)">查看</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="delFun">禁用</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="delFun(1)">启用</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button  @click="handleExport">导出</el-button>
              <!-- <el-button :disabled="selectedList.length !=1" @click="auditAuthFun">审核</el-button> -->
            </el-col>
            <el-col :span="1.5">
              <!-- <el-button :disabled="selectedList.length !=1" @click="medicalRepresentativeactiveFun">协议审核通过</el-button> -->
            </el-col>
          </el-row>
        <VxeTableGrid
          style="flex: 1;"
          :tableColumn="tableColumn"
          :tableLoading="tableLoading"
          :dataList="dataList"
          :showPagination="true"
          :paginationParams="paginationParams"
          @getList="setQueryParams"
          @selectedChange="selectedChange"
        >
        </VxeTableGrid>
      </div>
      
     </template>
   </PanelSplitTwo>
</template>
<script setup name="DepartmentList" lang="jsx">
import { useRouter } from 'vue-router'
import { medicalRepresentativeList, delMedicalRepresentative, delMedicalRepresentativeenable, auditAuth, medicalRepresentativeactive  } from "@/api/basicManagement/medicalRepresentative";


import { ElMessageBox } from 'element-plus'
const { proxy } = getCurrentInstance();
const { medical_represent_status } = proxy.useDict("medical_represent_status");

import datas from './data.js'

const keys = ref(0)
const Id = ref('')
const { paginationParams, orderSelect }  = datas
const tableColumn = ref([
  { type: "checkbox", width: 60, fixed: null },
  { field: "name", title: "姓名" },
  { field: "phone", title: "电话" },
  { field: "idCard", title: "身份证号码" },
  { field: "sex", title: "性别",
  slots: { default: ({ row }, h) => {
    return row.sex == 1? '女' : '男'
  }} },
  { field: "agencyName", title: "代理商" },
  { field: "isDeleted", title: "是否禁用",
  slots: { default: ({ row }, h) => {
    return row.isDeleted == 0? '否' : '是'
  }} },
  { field: "serviceScope", title: "服务范围" },
  { field: "authorizedHospital", title: "推广医院" },
  // { field: "status", title: "状态",
  // slots: { default: ({ row }, h) => {
  //   return [<dict-tag options={ medical_represent_status.value } value={ row.status }></dict-tag>]
  // }} }
])

const handleExport = () => {
  proxy.download("/base/medicalRepresentative/export", {
    ...paginationParams.value
  }, `config_${new Date().getTime()}.xlsx`);
}
watch(
  medical_represent_status,
  (newV, oldV) => {
    orderSelect[2].selectData = medical_represent_status.value;
    keys.value = new Date().getTime();
  }
);


const router= useRouter ()



const pageSize = ref(10);
let tableLoading = ref(false)
const dataList = ref([
])

// 切换分页
  function setQueryParams(val) {
    paginationParams.value.pageNum = val.pageNum;
    paginationParams.value.pageSize = val.pageSize;
    initDate()
  }
  // 选择表格
  let selectedList = ref([])
  function selectedChange(val) {
    Id.value = val.currentItem.id
    selectedList.value = val.selectedList
    // this.form.customerId = val.currentItem.customerId;
  }
/** 搜索按钮操作 */
function handleQuery(type) {
  router.push({
    path: '/basicInformation/addMedicalRepresentative',
    query: {
      id: type == 1? Id.value : ''
    }
  })
}
const selectFunc = () => {
  paginationParams.value.pageNum = 1
  initDate()
}
const delFun = (type) => {
  (type == 1?delMedicalRepresentativeenable : delMedicalRepresentative)(Id.value).then(() => {
    proxy.$modal.msgSuccess(`${type == 1?'启用':'禁用'}成功`);
    initDate()
  })
  // ElMessageBox.confirm('是否确认禁用?')
  //   .then(() => {
      
  //   })
  //   .catch(() => {
  //     // catch error
  //   })
}
const initDate = () => {
  medicalRepresentativeList({...paginationParams.value, ...{isDeletedtrue: true}}).then((res) => {
    paginationParams.value.total = res.total
    dataList.value = res.rows
  })
}
initDate()
const auditAuthFun = () => {
  auditAuth({
    id: Id.value
  }).then(() => {
    proxy.$modal.msgSuccess(`审核成功`);
    initDate()
  })
}
const selChangeFun = (val) => {
  if (val.model == "authorizedHospital") {
    val.selectData.forEach((val) => {
      if (val.id == paginationParams.value.authorizedHospital) {
        paginationParams.value.authorizedHospital = val.name
      }
    })
    
  }
}
const medicalRepresentativeactiveFun = () => {
  medicalRepresentativeactive({
    id: Id.value
  }).then(() => {
    proxy.$modal.msgSuccess(`审核通过`);
    initDate()
  })
}
</script>
<style scoped>
  .app-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 85px);
    overflow: hidden;
  }
  .vex_flex {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
  }
  .flexmin {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
</style>